﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.IoUtils;
using SinaImgUpload.SainTool.DataEnhance.Sqlite;
using CatUtils.Utils.SpecialUtils.PicBed.Sina;
using System;
using System.Collections.Generic;
using CatUtils.Utils.SpecialUtils.View;

namespace SinaImgUpload.SainTool.LocalTool
{
    internal class UploadListFile
    {
        /// <summary>
        /// 批量上传lst文件
        /// </summary>
        /// <param name="FlodePath"></param>
        /// <param name="DeleteOld"></param>
        public void UpListFloder(string FlodePath, bool DeleteOld = true)
        {
            foreach (string Line in FloderNameHelper.GetFullFileNameList(FlodePath))
            {
                UpListFile(Line);
            }
        }

        /// <summary>
        /// 上传列表文件
        /// </summary>
        /// <param name="ListFile"></param>
        /// <param name="DeleteOld"></param>
        /// <returns></returns>
        public string UpListFile(string ListFile, bool DeleteOld = true)
        {
            List<string> ImgList = FileHelper.ReadAllLine(ListFile);
            int CompltedCount = 0;
            List<string> PicBedInfoList = new List<string>();

            foreach (string OrgImg in ImgList)
            {
                string SinaImgLink = "";
                if (OrgImg == "" || FileNameHelper.GetFileType(OrgImg) == ".zip")
                    continue;
                string TempImgLink = OrgImg;

                string SearchLink = new PicBedManger().GetPicBedLink(OrgImg);
                bool ImgExits = false;//如果图片存在，则不需要重复插入数据库
                if (SearchLink == "")
                {
                    SinaImgLink = SinaImgUploadTool.UploadImgLink(TempImgLink);
                    if (SinaImgLink == "")
                    {
                        TempImgLink = TempImgLink.Replace(".jpg", ".png");//这个是因为Z站有jpg和Png，需要发起重试。
                        SinaImgLink = SinaImgUploadTool.UploadImgLink(TempImgLink);
                        Console.WriteLine(TempImgLink);
                    }
                }
                else
                {
                    Console.WriteLine("查询缓存得到结果：" + SearchLink);
                    SinaImgLink = string.Format("https://wx3.sinaimg.cn/large/{0}", SearchLink);
                    ImgExits = true;
                }
                if (SinaImgLink == "")
                    continue;
                if (!ImgExits)
                    new PicBedManger().UpdateValue(OrgImg, SinaImgLink, false.ToString());
                PicBedInfoList.Add(string.Format(SinaStaticvalue.ImgInfoText, SinaImgLink, OrgImg));
                new PrintLog().PercentLog(ImgList.Count, ++CompltedCount);
            }
            string NewFileName = FileNameHelper.CreateFileNameWithChildFloder(ListFile, "上传到图床").Replace(".lst", ".acg");
            FileHelper.WriteList(NewFileName, PicBedInfoList);
            if (DeleteOld)
                FileHelper.FileDelete(ListFile);

            return NewFileName;
        }
    }
}